package com.mobilesales.android.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

import com.mobilesales.android.model.params.FieldType;
import com.mobilesales.android.model.params.Param;
import com.mobilesales.android.model.params.ParamType;

public class ParamManager extends ManagerBase {
	
	public ParamManager(Context c)
	{
		super(c);
	}
	
	public List<Param> GetParamList(ParamType type)
	{		
     	try {
     		Log.d("get param list", type.toString());
     		if (type == ParamType.Employee) throw new UnsupportedOperationException();
     		
     		String query = "SELECT _id"
     						+ " FROM ParamTypes"
     						+ " WHERE Name = \"" + type.toString() +"\"";
     		Cursor c = db.rawQuery(query, null);     		
     		List<Param> result = new ArrayList<Param>();
     		if (!c.moveToFirst())
     			return result;
     		
     		int typeId = c.getInt(c.getColumnIndex("_id"));
     		c.close();
     		query = "SELECT _id"
					+ " FROM Params"
					+ " WHERE ParamTypeId = " + typeId;
     		
     		c = db.rawQuery(query, null);
     		if (!c.moveToFirst())
     			return result;
     		
     		List<Integer> tmp = new ArrayList<Integer>();
     		while (!c.isAfterLast()){
     			tmp.add(c.getInt(c.getColumnIndex("_id")));
     			c.moveToNext();
     		}
     		c.close();
     		for (int id : tmp)
     			result.add(GetParam(id));
     		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage(),sqle);
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage(), e);
     	}
     	
		return null;
	}
	
	public Param GetParam(int paramId)
	{
     	try {
     		
     		String query = "SELECT _id, ParamTypeId, Name, Description, ReadOnly, Required, FieldType, DefaultValue, MaxLength, DisplayName" 
							+ " FROM Params" 
							+ " WHERE _id = " + paramId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Param result = new Param();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.ParamType = c.getInt(c.getColumnIndex("ParamTypeId")) >= 0 ? ParamType.values()[c.getInt(c.getColumnIndex("ParamTypeId"))-1] : null;
     		result.Name = c.getString(c.getColumnIndex("Name"));
     		result.Description = c.getString(c.getColumnIndex("Description"));
     		result.IsReadOnly = c.getInt(c.getColumnIndex("ReadOnly")) == 1 ? true : false;
     		result.IsRequired = c.getInt(c.getColumnIndex("Required")) == 1 ? true : false;   		
     		result.FieldType = FieldType.valueOf(c.getString(c.getColumnIndex("FieldType")));
     		result.DefaultValue = c.getString(c.getColumnIndex("DefaultValue"));
     		result.MaxLength = c.getInt(c.getColumnIndex("MaxLength"));
     		result.DisplayName = c.getString(c.getColumnIndex("DisplayName"));
		  		
     		c.close();
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage(),sqle);
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage(), e);
     	}
		return null;
	}
	
	public boolean StoreParam(Param param)
	{

     	try {
     		int result;
			ContentValues cv = new ContentValues();

			cv.put("_id", param.Id);
			cv.put("Name",param.Name);
			cv.put("Description", param.Description);
			cv.put("ReadOnly", param.IsReadOnly);
			cv.put("Required", param.IsRequired);
			cv.put("FieldType", param.FieldType.toString());
			cv.put("DefaultValue", param.DefaultValue);
			cv.put("MaxLength", param.MaxLength);
			cv.put("DisplayName", param.DisplayName);
			cv.put("ParamTypeId", param.ParamType.ordinal()+1);
			
			result = db.update("Params", cv, "_id ="+param.Id, null);
			Log.d("param update result", "id:"+param.Id+" name:"+param.Name+" "+result);
			if (result == 0) {
				result = db.insert("Params", null, cv) != -1 ? 1 : 0;
				Log.d("param insert result ", "id:"+param.Id+" name:"+param.Name+" "+result);
			}
			
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage(),sqle);
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage(), e);
     		return false;
     	}
	}
	
	public boolean RemoveParam(int paramId)
	{
     	try {
     		int result;
			result = db.delete("Params", "_id ="+paramId, null);
			return result == 1 ? true : false;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage(),sqle);
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage(), e);
     		return false;
     	}
	}
}
